package com.geovis.emergency.spd.controller.iflytek;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.geovis.common.auth.util.AuthUtil;
import com.geovis.common.core.api.Result;
import com.geovis.emergency.spd.biz.iflytek.config.IflytekConfig;
import com.geovis.emergency.spd.biz.iflytek.service.IflytekService;
import com.geovis.emergency.spd.biz.iflytek.thread.PushThread;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillService;
import com.geovis.emergency.spd.biz.pc.service.IPcPlanService;
import com.geovis.emergency.spd.biz.system.service.ISystemFileService;
import com.geovis.emergency.spd.cache.UserCache;
import com.geovis.emergency.spd.entity.pc.entity.PcDrill;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcPlanDetailVO;
import com.geovis.emergency.spd.entity.system.entity.SystemUser;
import com.geovis.emergency.spd.props.SsoProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/8/17
 */
@RestController
@RequestMapping("/iflytek")
@RequiredArgsConstructor
@Api(value = "科大-相关接口", tags = "科大-相关接口")
@Slf4j
@Validated
public class IflytekControler  {

    private final SsoProperties ssoProperties;
    private final IPcDrillService pcDrillService;
    private final IflytekService iflytekService;

    @ApiOperation(value = "ssoAuth", notes = "sso 授权")
    @PostMapping("/ssoAuth")
    public Result<String> ssoAuth(){
        String taiId = "80";
        String redirectUri = ssoProperties.getRedirectUri();
        String finalUrl = ssoProperties.getFinalUrl();
        String hzdUserId = AuthUtil.getCurrUser().getUserId();
        String hzdDeptId = AuthUtil.getCurrUser().getDeptId();
        String validTime = DateUtil.formatDateTime(new Date());
        String appKey = ssoProperties.getAppKey();
        String appPwd = ssoProperties.getAppPwd();
        String ticket = SmUtil.sm3(SmUtil.sm3(taiId + redirectUri + finalUrl) + SmUtil.sm3(appKey + appPwd + validTime) + SmUtil.sm3(hzdUserId + hzdDeptId));

        Map<String,String> param = new HashMap<>(8);
        param.put("appKey",appKey);
        param.put("taiId",taiId);
        param.put("redirectUri",redirectUri);
        param.put("finalUrl",finalUrl);
        param.put("hzdUserId",hzdUserId);
        param.put("hzdDeptId",hzdDeptId);
        param.put("validTime",validTime);
        param.put("ticket",ticket);
        final String jsonStr = JSONUtil.toJsonStr(param);
        log.error("jsonStr {}",jsonStr);
        String result = HttpUtil.post(ssoProperties.getLoginUrl(), jsonStr);
        JSONObject data = JSONObject.parseObject(result);
        if (ObjectUtil.isNotNull(data)){
            if (data.getInteger("status") == 200){
                return Result.ok(data.getString("data"));
            }
            return Result.failed(data.getString("message"));
        }
        return Result.failed("授权失败");
    }
    @ApiOperation(value = "pushDrill", notes = "推送演练到科大")
    @PostMapping("pushDrill/{drillId}")
    public Result pushDrill(@PathVariable("drillId") String drillId){
        PcDrill pcDrill = pcDrillService.getById(drillId);
        if (ObjectUtil.isNotNull(pcDrill)){
            PushThread.pushDrill(pcDrill);
        }
        return Result.ok();
    }
    @ApiOperation(value = "graphData", notes = "获取演练关系图谱")
    @PostMapping("graphData/{drillId}")
    public Result graphData(@PathVariable("drillId") String drillId){
        PcDrill pcDrill = pcDrillService.getById(drillId);
        if (ObjectUtil.isNotNull(pcDrill) && StrUtil.isNotBlank(pcDrill.getPushId())){
            JSONObject graphData = iflytekService.getGraphData(pcDrill.getPushId());
            if (ObjectUtil.isNotNull(graphData)){
                return Result.ok(graphData);
            }
        }
        return Result.ok();
    }
}
